package com.github.baichuan.application.scaffold.controller;

import com.github.baichuan.application.scaffold.domain.authorization.MenuService;
import com.github.baichuan.application.scaffold.domain.authorization.Resource;
import com.github.baichuan.application.scaffold.domain.authorization.ResourceService;
import com.github.baichuan.application.scaffold.dto.resource.MenuForm;
import com.github.baichuan.application.scaffold.dto.resource.TreeMapper;
import com.github.baichuan.application.scaffold.dto.resource.TreeNode;
import com.github.baichuan.mapping.BeanMapper;
import com.github.baichuan.web.dto.Result;
import com.github.baichuan.web.security.Authorization;
import com.github.baichuan.web.servlet.Restful;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "菜单")
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private ResourceService resourceService;

    @GetMapping("/navigation")
    @Operation(tags = "获取用户被授权的菜单树")
    public ResponseEntity<List<TreeNode>> authorization(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                        @RequestHeader("x-member-uid") String uid){
        List<Resource> menus = menuService.getAuthorized(application, uid);
        TreeMapper treeMapper = new TreeMapper();
        List<TreeNode> views = treeMapper.map(menus);

        return ResponseEntity.ok(views);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取表单数据")
    public ResponseEntity<MenuForm> get(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                              @PathVariable("id") String id) {
        Resource menu = menuService.find(application, id);
        MenuForm form = BeanMapper.map(menu, MenuForm.class);
        return ResponseEntity.ok(form);
    }

    @PostMapping
    @Operation(summary = "添加")
//    @Authorization("menu:add")
    public ResponseEntity<Result> add(@RequestHeader("x-application-code") @Parameter(hidden = true) String application, @RequestBody @Valid MenuForm form){
        Result result = menuService.add(application, form);
        return Restful.created(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改")
    @Authorization("menu:edit")
    public ResponseEntity<Result> edit(@RequestHeader("x-application-code") @Parameter(hidden = true) String application, @PathVariable("id") String id,
                               @RequestBody @Valid MenuForm form){
        Result result = menuService.edit(application, id, form);
        return Restful.response(result);
    }

//    @DeleteMapping("/{id}")
//    @Operation(summary = "删除")
//    @Authorization("menu:remove")
//    public ResponseEntity<Result> remove(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
//                                         @PathVariable("id") String id){
//        Result result = resourceService.remove(application, id);
//        return Restful.deleted(result);
//    }
}
